﻿using QuanLySieuThi.Controllers;
using QuanLySieuThi.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLySieuThi.Views
{
    public partial class frmNhapHang : Form
    {
        public frmNhapHang()
        {
            InitializeComponent();
            maHoaDonNhap = string.Format("{0}{1}{2}{3}", DateTime.Now.DayOfYear, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Millisecond);
        }
        private string maHoaDonNhap;
        private void click_Nhap(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            int r = e.RowIndex;
            string maMatHang = "";
            if (dataGridView1.Rows[r].Cells["MaMatHang"].Value != null)
                maMatHang = dataGridView1.Rows[r].Cells["MaMatHang"].Value.ToString();
            MatHang m = QuanLyMatHang.UniqueObject.LayTheoMa(maMatHang);
          

            if (e.RowIndex >= 0 && m!=null)
            {
                dataGridView1.Rows[r].Cells[2].Value = m.MaLoaiHang;
                dataGridView1.Rows[r].Cells[1].Value = m.TenMatHang;
                dataGridView1.Rows[r].Cells[5].Value = m.GiaMua;
                dataGridView1.Rows[r].Cells[4].Value = m.HanSuDung;
                dataGridView1.Rows[r].Cells[3].Value = m.NgaySanXuat;

                int donGia, soLuong;
                //Thanh tiền = Đơn giá *  số lượng
                if (dataGridView1.Rows[r].Cells["DonGia"].Value != null
                    && dataGridView1.Rows[r].Cells["SoLuong"].Value != null
                    && int.TryParse(dataGridView1.Rows[r].Cells["DonGia"].Value.ToString(), out donGia)
                     && int.TryParse(dataGridView1.Rows[r].Cells["SoLuong"].Value.ToString(), out soLuong))
                {
                    dataGridView1.Rows[r].Cells["ThanhTien"].Value = soLuong * donGia;
                    int tong = 0;
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if (dataGridView1.Rows[i].Cells["ThanhTien"].Value != null)
                        {
                            tong += int.Parse(dataGridView1.Rows[i].Cells["ThanhTien"].Value.ToString());
                        }
                    }
                    txtTongTien.Text = tong.ToString();
                }
            }

            if (e.ColumnIndex != 0 && dataGridView1.Rows[r].Cells["MaMatHang"].Value == null)
            {
                MessageBox.Show("Điền mã mặt hàng!");
            }
        }

        private void txtMaNCC_TextChanged(object sender, EventArgs e)
        {
            NhaCungCap ncc = QuanLyNCC.UniqueObject.LayTheoMa(txtMaNCC.Text);
            if (ncc != null)
            {
                txtDiaChi.Text = ncc.DiaChi;
                txtSDT.Text = ncc.SDT;
                txtTenNCC.Text = ncc.TenNhaCungCap;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Nhập hóa đơn nhâpk
            QuanLyHoaDonNhap.UniqueObject.Add(GetHoaDonNhapFromView());
            //Nhập chi tiết hóa đơn nhập
            List<HoaDonChiTietNhap> l = GetListHoaDonChiTietNhapFromView();
            foreach (var item in l)
                QuanLyHoaDonChiTietNhap.UniqueObject.Add(item);

            MessageBox.Show("OK");
        }

        private HoaDonNhap GetHoaDonNhapFromView()
        {
            HoaDonNhap h = new HoaDonNhap();
            h.MaHoaDonNhap = maHoaDonNhap;
            h.MaNhaCungCap = txtMaNCC.Text;
            h.MaNhanVien = frmMain.userLogin.MaNhanVien;
            h.ThoiGian = DateTime.Now;
            h.TongTien = int.Parse(txtTongTien.Text);
            return h;
        }

        private List<HoaDonChiTietNhap> GetListHoaDonChiTietNhapFromView()
        {
            List<HoaDonChiTietNhap> l = new List<HoaDonChiTietNhap>();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                var r = dataGridView1.Rows[i];
                if (r.Cells["MaMatHang"].Value != null 
                    && r.Cells["SoLuong"].Value != null
                    && r.Cells["DonGia"].Value != null
                    )
                {
                    HoaDonChiTietNhap h = new HoaDonChiTietNhap();
                    h.GiaMua = int.Parse(r.Cells["DonGia"].Value.ToString());
                    h.MaHoaDonNhap = maHoaDonNhap;
                    h.MaMatHang = r.Cells["MaMatHang"].Value.ToString();
                    h.SoLuong = int.Parse(r.Cells["SoLuong"].Value.ToString());
                    l.Add(h);
                }
            }
            return l;
        }

        private void frmNhapHang_Load(object sender, EventArgs e)
        {

        }
    }
}
